package cn.gzsxt.controller;

import java.io.IOException;
import java.util.regex.Pattern;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import cn.gzsxt.po.Tenant;
import cn.gzsxt.service.TenantService;
import cn.gzsxt.service.impl.TenantServiceImpl;
import cn.gzsxt.vo.MsgObj;

@WebServlet("/handlerTenant.gzsxt")
public class HandlerTennatController extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	
	//引入service层
	private TenantService service = new TenantServiceImpl();
	
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//接受标识参数 : addTenant , listTenant, deleteTenant,updateTenant
		String cmd = req.getParameter("cmd");
		if("addTenant".equals(cmd)){
			addTenant(req,resp);
		}
		
		
	}

	//添加逻辑
	private void addTenant(HttpServletRequest req, HttpServletResponse resp) 
			throws ServletException, IOException{
		//接受 公司名称,法人,电话,身份证,身份证正面的图片地址,身份证反面图片地址 //TODO 完成营业执照
		String companyname = req.getParameter("companyname");
		String managername = req.getParameter("managername");
		String phone = req.getParameter("phone");
		String identity = req.getParameter("identity");
		String identitypositive = req.getParameter("identitypositive");
		String identitynegative = req.getParameter("identitynegative");
		//正则校验: Pattern类
		//boolean matches = Pattern.matches("正则规则", "要校验的字符串");//true/false
		//封装成对象
		Tenant tenant = new Tenant();
		tenant.setCompanyname(companyname);
		tenant.setManagername(managername);
		tenant.setPhone(phone);
		tenant.setIdentity(identity);
		tenant.setIdentitypositive(identitypositive);
		tenant.setIdentitynegative(identitynegative);
		
		//调用service 保存方法
		int row = service.addTenant(tenant);
		
		//创建MsgObj 消息对象
		MsgObj msgObj = new MsgObj(0, "添加租户数失败");
		if(row == 1){
			msgObj = new MsgObj(1, "添加租户数成功");
		}
		//设置响应MIME和字符编码
		resp.setContentType("application/json;charset=utf-8");
		
		//将对象响应给前台
		resp.getWriter().print(JSON.toJSONString(msgObj));
	}

}
